

class UserAccountController {

  def index = { redirect(action:login,params:params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [delete:'POST', save:'POST', update:'POST', logincheck:'POST']  

  def login = {     
  
    if (request.get)
      render(view: 'login')
    else {
      def userAccount = UserAccount.findByEmail(params.email)
      if(userAccount){
        if (params.password.equals(userAccount.password)) {
          session.setAttribute("userAccount",userAccount)
          redirect(uri:"/")  
          //render (view: "${createLinkTo(dir:'')}")
          //redirect(controller: 'event', action: 'list')
        } else {
          flash.message = "Invalid login"
          render view: 'login'
        }
      } else {
        flash.message = "No User found for email: ${params.email}"
        render view: 'login'
      }
    }
  }


  def show = {
    def userAccountInstance = session.getAttribute("userAccount")

    if(!userAccountInstance) {
      flash.message = "UserAccount not found in the session"
       redirect(uri:"/")
    }
    else { return [ userAccountInstance : userAccountInstance ] }
  }

  def delete = {
    def userAccountInstance = UserAccount.get( params.id )
    if(userAccountInstance) {
      try {
        userAccountInstance.delete()
        flash.message = "UserAccount ${params.id} deleted"
        redirect(action:list)
      }
      catch(org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "UserAccount ${params.id} could not be deleted"
        redirect(action:show,id:params.id)
      }
    }
    else {
      flash.message = "UserAccount not found with id ${params.id}"
      redirect(action:list)
    }
  }

  def edit = {
    def userAccountInstance = session.getAttribute("userAccount")

    if(!userAccountInstance) {
      flash.message = "UserAccount not found in the session"
       redirect(uri:"/")
    }
    else {
      return [ userAccountInstance : userAccountInstance ]
    }
  }

  def update = {
    def userAccountInstance = UserAccount.get( params.id )
    def userAccountSession = session.getAttribute("userAccount")

    if(!(userAccountInstance.getEmail().equals(userAccountSession.getEmail()))){
      render ("You are not authorized to perform this function")
      return
    }

    if(userAccountInstance) {
      if(params.version) {
        def version = params.version.toLong()
        if(userAccountInstance.version > version) {

          userAccountInstance.errors.rejectValue("version", "userAccount.optimistic.locking.failure", "Another user has updated this UserAccount while you were editing.")
          render(view:'edit',model:[userAccountInstance:userAccountInstance])
          return
        }
      }
      userAccountInstance.properties = params
      if(!userAccountInstance.hasErrors() && userAccountInstance.save()) {

         if(session.getAttribute("userAccount")){
         session.setAttribute("userAccount",userAccountInstance)
         }
        
        flash.message = "UserAccount ${params.id} updated"
        redirect(action:show,id:userAccountInstance.id)
      }
      else {
        render(view:'edit',model:[userAccountInstance:userAccountInstance])
      }
    }
    else {
      flash.message = "UserAccount not found with id ${params.id}"
      redirect(action:edit,id:params.id)
    }
  }

  def create = {
    def userAccountInstance = new UserAccount()
    userAccountInstance.properties = params
    return ['userAccountInstance':userAccountInstance]

  }

  def save = {
    def userAccountInstance = new UserAccount(params)
    if(!userAccountInstance.hasErrors() && userAccountInstance.save()) {

     //check if account exsits
      if(session.getAttribute("userAccount")){
        session.setAttribute(userAccountInstance)
               redirect action:'index'
      }
      else{
        sendMail {
                    to userAccountInstance.email
                    subject "Welcome to Event Manager!"
                     html g.render(template:"EmailTemplate", model: [userAccountInstance: userAccountInstance])
                }
                flash.message = "Your UserAccount is Created and Email sent to ${userAccountInstance.email}!"
          // flash.message = "Your UserAccount is created"
             redirect(action:login,id:userAccountInstance.id)
      }
    }
    else {
      render(view:'create',model:[userAccountInstance:userAccountInstance])
    }
  }

  def logout = {
        session.invalidate()
		redirect(action:"login")
  }
}
